package com.ht.interfaces.request.link;

import com.ht.api.db.Db;
import com.ht.api.db.Entity;
import com.ht.api.db.annotate.FuncCall;
import com.ht.api.db.annotate.FuncGroup;
import com.ht.api.db.annotate.FuncId;
import com.ht.api.db.annotate.FuncName;
import com.ht.api.db.annotate.FuncParam;
import com.ht.api.db.annotate.FuncParams;
import com.ht.api.db.bean.DataResult;
import com.ht.api.db.bean.SessionBean;
import com.ht.api.db.request.IHandleRequest;
import com.ht.api.db.util.MqUtil;
import com.ht.interfaces.utils.CallTestUtil;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@FuncId("1912831280471478272")
@FuncGroup("H5接口")
@FuncParams({
	@FuncParam(name = "资料ID", code = "lesson_id", value = "1914509485364350976"),
	@FuncParam(name = "广告任务ID", code = "task_id", value = "1912439849445625856"),

})

@FuncCall
@FuncName("领取资料")
public class ReceiveLesson implements IHandleRequest {

	@Override
	public void execute(SessionBean me, JSONObject param, DataResult result) {

		List<Entity> dataList = new ArrayList<>();

		String time = DateUtil.now();

		Entity lessonReceiveLog = Entity.create("lesson_receive_log");
		lessonReceiveLog.set("openid", me.getOpenid());
		lessonReceiveLog.set("lesson_id", param.get("lesson_id"));
		lessonReceiveLog.set("time", time);
		dataList.add(lessonReceiveLog);

		Entity userEventLog = Entity.create("user_event_log");
		userEventLog.set("openid", me.getOpenid());
		userEventLog.set("event_type", "lesson");
		userEventLog.set("event_key", "get");
		userEventLog.set("relation_id", param.get("lesson_id"));
		userEventLog.set("relation_type", 1);
		userEventLog.set("event_time", time);
		userEventLog.set("event_msg", "");
		String context = Db.createSqlParser("ad_user_task").select("context").where("main_id", param.get("task_id")).val(me.getDataSource());
		userEventLog.set("context", context);
		dataList.add(userEventLog);

		MqUtil.send(me.getTenantId(), dataList);

		result.setMsg("领取资料成功，稍后整理好了直接发你哦~");
	}

	public static void main(String[] args) throws Exception {

		CallTestUtil.autoTest(new Object(){}.getClass().getEnclosingClass().getSimpleName());
	}
}
